import { ComponentInfo } from '../domain/component-info'
import path from 'path'
import { readFileSync } from 'node:fs'
import _ from 'lodash'

const filePathPrefix = '../template/'

const templateFileMap = {
  sfcTemplate: {
    filePath: 'component.sfc.tpl'
  },
  tsxTemplate: {
    filePath: 'component.tsx.tpl'
  },
  indexTemplate: {
    filePath: 'component.index.tpl'
  },
  sassTemplate: {
    filePath: 'component.sass.tpl'
  },
  typesTemplate: {
    filePath: 'component.type.tpl'
  },
  docsDemoTemplate: {
    filePath: 'vitePress.docs.demo.tpl'
  },
  docsTemplate: {
    filePath: 'vitePress.docs.tpl'
  }
}

/**
 * .vue 文件模板
 */
export const sfcTemplate = (lineNameWithPrefix: string, lowCamelName: string): string => {
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.sfcTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ lineNameWithPrefix, lowCamelName })
}

/**
 * .tsx 文件模板
 */
export const tsxTemplate = (lineNameWithPrefix: string, lowCamelName: string): string => {
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.tsxTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ lineNameWithPrefix, lowCamelName })
}

/**
 * types.ts 文件模板
 */
export const typesTemplate = (lowCamelName: string, upCamelName: string): string => {
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.typesTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ upCamelName, lowCamelName })
}

/**
 * 组件入口 index.ts 文件模板
 */
export const indexTemplate = (componentInfo: ComponentInfo): string => {
  const { upCamelName } = componentInfo
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.indexTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ upCamelName })
}

/**
 * scss 文件模板
 */
export const scssTemplate = (lineNameWithPrefix: string): string => {
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.sassTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ lineNameWithPrefix })
}

export const mdTemplate = (componentInfo: ComponentInfo) => {
  const { upCamelName, zhName, lineName } = componentInfo
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.docsTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ upCamelName, zhName, lineName })
}

export const demoTemplate = (lineNameWithPrefix: string) => {
  const content = readFileSync(path.resolve(__dirname, filePathPrefix, templateFileMap.docsDemoTemplate.filePath), 'utf-8')
  const compiled = _.template(content)
  return compiled({ lineNameWithPrefix })
}
